package com.laosg.framework.fhir.springboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.data.cassandra.config.AbstractCassandraConfiguration;
import org.springframework.data.cassandra.config.CassandraClusterFactoryBean;
import org.springframework.data.cassandra.config.CassandraCqlClusterFactoryBean;
import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by kaimin on 4/7/2019.
 * time : 22:34
 */
public class CassandraConfig extends AbstractCassandraConfiguration {

    private static final String KEYSPACE_NAME = "cassandra";
    private static final String HOSTNAME = "0.0.0.0";
    private static final int PORT = 9042;

    @Override
    protected String getKeyspaceName() {
        return KEYSPACE_NAME;
    }

    @Bean
    public CassandraClusterFactoryBean cluster() {
        CassandraCqlClusterFactoryBean bean = new CassandraCqlClusterFactoryBean();
        bean.setContactPoints(HOSTNAME);
        bean.setPort(PORT);
        bean.setKeyspaceCreations(getKeyspaceCreations());
        return bean;
    }

    @Override
    protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {
        List<CreateKeyspaceSpecification> createKeyspaceSpecifications =
                new LinkedList();
        createKeyspaceSpecifications.add(getKeyspaceSpecification());
        return createKeyspaceSpecifications;
    }

    // Create keyspace if it doesn't already exist
    private CreateKeyspaceSpecification getKeyspaceSpecification() {
        return CreateKeyspaceSpecification.createKeyspace(getKeyspaceName())
                .ifNotExists(true);
    }

    @Override
    public String[] getEntityBasePackages() {
        return new String[] {"org.hl7.fhir.dstu3.model"};
    }

    @Override
    public SchemaAction getSchemaAction() {
        return SchemaAction.RECREATE_DROP_UNUSED;
    }



}
